package com.huatai.datacenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.huatai.common.api.R;
import com.huatai.datacenter.entity.DataSourceEntity;
import com.huatai.datacenter.service.DataSourceService;
import com.huatai.datacenter.service.SyncTaskService;
import com.huatai.datacenter.utils.CryptoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Lion
 * @date 2023/3/13  10:26
 */
@RestController
@Api(value = "数据源管理",tags = "数据源管理")
@RequestMapping("/dataSource")
public class DataSourceController extends BaseController<DataSourceService,DataSourceEntity> {

	@Autowired
	private DataSourceService dataSourceService;

	@PostMapping("/saveDataSource")
	@ApiOperation("新增或更新数据源")
	public R saveOrUpdateDataSource(@RequestBody DataSourceEntity dataSource){

		if (dataSource.getId() == null){
			dataSource.setPassword(CryptoUtil.encode(dataSource.getPassword()));
		}else {
			//判断是否修改密码
			String desPassword = super.service.getById(dataSource.getId()).getPassword();
			if (!desPassword.equals(dataSource.getPassword())){
				dataSource.setPassword(CryptoUtil.encode(dataSource.getPassword()));
			}
		}
		return super.save(dataSource);
	}

	@GetMapping("/getDataSourceList")
	@ApiOperation("获取数据源列表")
	public R queryDataSourceList(){

		return R.data(dataSourceService.list());
	}

	@PostMapping("/deleteDataSourceList")
	@ApiOperation("根据Id删除数据源")
	public R deleteDataSource(@RequestBody List<Integer> list){

		return R.status(dataSourceService.removeBatchByIds(list));
	}

	@ApiImplicitParams({
		@ApiImplicitParam(name = "dataSourceType", value = "数据源类型", dataType = "String"),
		@ApiImplicitParam(name = "connectName", value = "连接名称", dataType = "String"),
		@ApiImplicitParam(name = "url", value = "url", dataType = "String"),
		@ApiImplicitParam(name = "username", value = "username", dataType = "String"),
		@ApiImplicitParam(name = "password", value = "password", dataType = "long"),
		@ApiImplicitParam(name = "databaseType", value = "数据源类型", dataType = "String"),
		@ApiImplicitParam(name = "driver", value = "driver", dataType = "String"),
		@ApiImplicitParam(name = "tableSql", value = "获取所有表名的SQL", dataType = "String"),
		@ApiImplicitParam(name = "databaseName", value = "数据库名", dataType = "String")
	})
	@PostMapping("/connect")
	@ApiOperation("测试数据源连接")
	public R DataSource(@RequestBody DataSourceEntity dataSource){

		boolean status = dataSourceService.connect(dataSource);

		if (status){
			return R.status(true);
		}else {
			return R.status(false);
		}
	}

	@ApiOperation("根据数据源id获取到所有表名")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "dataSourceId", value = "数据源id", dataType = "long")
	})
	@PostMapping("/queryAllTableName")
	public R queryAllTableNames(@RequestBody JSONObject data){
		long dataSourceId = data.getLong("dataSourceId");

		return R.data(dataSourceService.queryAllTables(String.valueOf(dataSourceId)));
	}

	@ApiImplicitParams({
		@ApiImplicitParam(name = "dataSourceId", value = "数据源id", dataType = "long"),
		@ApiImplicitParam(name = "tableName", value = "表名", dataType = "String")
	})
	@ApiOperation("根据数据源id和表名获取所有列名")
	@PostMapping("/getAllColumns")
	public R queryAllColumns(@RequestBody JSONObject data){

		long dataSourceId = data.getLong("dataSourceId");
		String tableName = data.getString("tableName");
		List<JSONObject> result = dataSourceService.getALLColumns(dataSourceId,tableName);

		return R.data(result);
	}

	/**
	 * @author Lion
	 * @description 数据库中所有表以及表中数据的复制
	 * @param  data
	 * @return com.huatai.common.api.R
	 */
	@ApiImplicitParams({
		@ApiImplicitParam(name = "dataSourceId", value = "源数据源id", dataType = "long"),
		@ApiImplicitParam(name = "targetId", value = "目标数据源id", dataType = "long"),
		@ApiImplicitParam(name = "tableName", value = "表名", dataType = "String"),
	})
	@PostMapping("/moveTableData")
	@ApiOperation("复制表结构及数据")
	public R copyDataTable(@RequestBody JSONObject data) throws SQLException {

		dataSourceService.moveTableData(data);

		return R.success("");
	}

	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "源数据源id", dataType = "long")
	})
	@PostMapping("/getDataById")
	@ApiOperation("根据id获取数据源")
	public R queryDataById(@RequestBody JSONObject data) throws SQLException {

		long id = data.getLong("id");
		return R.data(super.service.getById(id));
	}
}
